

from django.shortcuts import HttpResponse, redirect, render
from django.urls import reverse
from web.myform.wiki import WikiModelForm
from web import models
from django.http import JsonResponse
from django.views.decorators.csrf import csrf_exempt, csrf_protect
# csrf_exempt装饰的接口，不会进行csrftoken认证，即便是全局配置配置了认证中间件
# csrf_protect装饰的接口，必须通过csrftoken认证，即便是全局没有配置认证中间件
from libs.tengxunyun.cos import upload_file


def wiki(request, pro_id):
    # /wiki/?wiki_id = 1
    wiki_id = request.GET.get('wiki_id')
    wiki_obj = models.Wiki.objects.filter(id=wiki_id).first()
    # print('xxxxxx')
    return render(request, 'wiki.html',{'wiki_obj': wiki_obj})


def wiki_add(request, pro_id):

    if request.method == 'GET':
        form = WikiModelForm(request)
        return render(request, 'wiki_form.html', {'form': form})
    else:

        form = WikiModelForm(request, data=request.POST)

        if form.is_valid():
            # 判断用户是否选择了父级文章
            if form.instance.parent:
                form.instance.depth = form.instance.parent.depth + 1
            else:
                form.instance.depth = 1

            form.instance.project_id = pro_id
            form.save()

            # url别名反向解析
            url = reverse('wiki', kwargs={'pro_id': pro_id})
            return redirect(url)
        print('>>>>>', form.errors)
        return render(request, 'wiki_form.html', {'form': form})


# 编辑wiki
def wiki_edit(request, pro_id, wiki_id):

    if request.method == 'GET':
        wiki_object = models.Wiki.objects.filter(project_id=pro_id, pk=wiki_id).first()

        form = WikiModelForm(request, instance=wiki_object)

        return render(request, 'wiki_form.html', {'form': form})
    else:
        wiki_object = models.Wiki.objects.filter(project_id=pro_id, pk=wiki_id).first()
        form = WikiModelForm(request, data=request.POST,instance=wiki_object)

        if form.is_valid():
            # 判断用户是否选择了父级文章
            if form.instance.parent:
                form.instance.depth = form.instance.parent.depth + 1
            else:
                form.instance.depth = 1

            form.instance.project_id = pro_id
            form.save()

            # url别名反向解析
            url = reverse('wiki', kwargs={'pro_id': pro_id})# + '/?wiki_id=1'
            # 调转回这个文章的详情页面
            preview_url = '{0}?wiki_id={1}'.format(url, wiki_id)
            return redirect(preview_url)
        # print('>>>>>', form.errors)
        return render(request, 'wiki_form.html', {'form': form})


def wiki_delete(request, pro_id, wiki_id):
    models.Wiki.objects.filter(project_id=pro_id, pk=wiki_id).delete()
    url = reverse('wiki', kwargs={'pro_id': pro_id})
    return redirect(url)



def wiki_catalog(request, pro_id):
    # 查询数据通过depth深度进行升序排列
    data = models.Wiki.objects.filter(project_id=pro_id).values('id', 'title', 'parent_id').order_by('depth')

    return JsonResponse({'status': True, 'data': list(data)})

@csrf_exempt
def wiki_upload(request, pro_id):

    '''响应给mdeditor的数据格式是固定的'''
    result = {
        'success': 0, #0表示失败，1表示成功
        'message':None,
        'url': ''  # 图片路径
    }

    print('有文件来啦！！！')
    print(request.FILES)
    img_obj = request.FILES.get('editormd-image-file')
    img_name = img_obj.name
    # 名字暂时先用用户本地上传时的图片名称
    # 后面应该改为唯一的图片名称，因为这个图片名称是用来作为存储桶存储我们文件时所叫的名称，如果重名了，会覆盖之前的文件，造成文件丢失

    url = upload_file('xiechen-1302729577', img_obj, img_name)

    result['success'] = 1
    result['url'] = url



    return JsonResponse(result)










